# 3 无重复字符的最长子串
# leetcode第3题: https://leetcode.cn/problems/longest-substring-without-repeating-characters/
# Date: 2024/10/15

"""滑动窗口中遍历的是窗口的大小"""


def lengthOfLongestSubstring(s: str) -> int:
    if not s:
        return 0
    max_len = 0
    start = 0  # 表示滑动窗口开始的位置
    for end in range(1, len(s)):
        win_str = s[start:end]  # 窗口内的字符串
        c = s[end]  # 窗口的下一个字符
        if c in win_str:
            # 不需要写 if 语句来判断大小
            max_len = max(max_len, end - start)
            start += win_str.index(c) + 1
    return max(max_len, len(s) - start)


if __name__ == '__main__':
    s1 = "abcabcbb"
    s2 = "bbbbb"
    s3 = "pwwkew"
    s4 = ""
    s5 = " "
    s6 = "ckilbkd"
    s7 = "ggububgvfk"
    print(lengthOfLongestSubstring(s1))
    print(lengthOfLongestSubstring(s2))
    print(lengthOfLongestSubstring(s3))
    print(lengthOfLongestSubstring(s4))
    print(lengthOfLongestSubstring(s5))
    print(lengthOfLongestSubstring(s6))
    print(lengthOfLongestSubstring(s7))
